﻿using System.Linq;
using CardPlay.Model.ActionModel;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Actions;

namespace CardPlay.DemoPack.Games.CongressVariants
{
    [Game("E15B6601-9588-44C8-915F-1A39662B40CC", LevelOfDifficulty.Easy, LevelOfSkill.ModerateSkill, DurationOfPlay.Within5To10Minutes, NumberOfDecks = 2, Family = FamilyName)]
    public class Dieppe : Congress
    {
        private int StockActionCount { get; set; }

        private bool InFirstGamePhase { get { return StockActionCount < 2; } }

        public Dieppe()
        {
            MaximumDeals = 1;
            TableauBuildSequence = CardFaceSequence.AnySuitDescendingRank;

        }

        protected override IDealAction CreateDealScript()
        {
            var script = new DealScript();
            script.DealAcesTo(StandardPileNames.Foundations.Take(8));

            var result = new DealSequence();

            result.Add(script.Compile());
            result.Add(base.CreateDealScript());

            return result;
        }

        protected override IAction QueryActionAt(PartLocation location)
        {
            var card = CardAt(location);
            if (card == null) return null;

            return location.Matches(Stock) && location.Index == this[Stock].TopIndex
                ? new ComplexAction
                    {
                        InFirstGamePhase
                            ? new DealOutTableauAction(this, location)
                            : base.QueryActionAt(location),
                        new DelegatingAction
                        {
                            OnExecute = delegate { ++StockActionCount; },
                            OnReverse = delegate { --StockActionCount; }
                        }
                    }
                : base.QueryActionAt(location);
        }

        protected override bool IsTableauPickupIsAllowed(RemovePileAction action)
        {
            return 
                action.Location.Matches(AnyTableau) &&
                InFirstGamePhase 
                    ? action.IsActingOnFaceUpTopCard()
                    : (action.IsActingOnFaceUpTopPile() && (action.PileBeingRemoved.Count == 1 || action.PileBeingRemoved.IsInSequence(CardFaceSequence.AnySuitDescendingRank)));
        }

        protected override bool AllowedToBuildEmptyTableau(MovePileAction action)
        {
            return ! InFirstGamePhase;
        }

        protected override bool AllowedToBuildNonEmptyTableau(MovePileAction action)
        {
            return !InFirstGamePhase && base.AllowedToBuildNonEmptyTableau(action);
        }

        protected override bool AllowedToBuildFoundation(MovePileAction action)
        {
            return action.IsBuildingTwoDeckFoundationWithIdenticalBase();
        }

        protected override void OnTransactionEnding()
        {
            if (InFirstGamePhase) this.FillFromStockEmptyPilesMatching(AnyTableau);
        }
    }
}